#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "com_lightsh_light_Light.h"
#include "signature_check.h"
/*
 * Class:     com_lightsh_light_Light
 * Method:    isOpen
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_com_lightsh_light_Light_isOpen
  (JNIEnv * env, jclass jc)
{
	if(!checkLegal())
	{
		return JNI_FALSE;
	}
	if(is40())
	{
		FILE * fe = fopen(PRO_4_ENABLE, "r");
		if(fe != NULL)
		{
			int cur = 0;
			fscanf(fe, "%d", &cur);
			fclose(fe);
			//LOGD("cur brightness = %d", cur);
			if(cur > 0)
			{
				return JNI_TRUE;
			}
		}
	}else
	{
		FILE * fe = fopen(PRO_ENABLE, "r");
		if(fe != NULL)
		{
			int enable = fgetc(fe);
			fclose(fe);
			//LOGD("e = %d", enable);
			if(enable > 0)
			{
				FILE * fm = fopen(PRO_MODE, "r");
				if(fm != NULL)
				{
					int mode = fgetc(fm);
					fclose(fm);
					//LOGD("m = %d", mode);
					if(mode == PRO_MODE_OPEN)
					{
						return 1;
					}
				
				}
			}
		}
	}
	return JNI_FALSE;
}

/*
 * Class:     com_lightsh_light_Light
 * Method:    open
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_lightsh_light_Light_open
  (JNIEnv * env, jclass jc)
{
	if(!checkLegal())
	{
		return;
	}
	if(is40())
	{
		if(!Java_com_lightsh_light_Light_isOpen(env, jc))
		{
			setLightCur(PRO_DEFAULT_CUR);
		}
	}else
	{
		FILE * fm = fopen(PRO_MODE, "w+");
		if(fm != NULL)
		{
			fputc(PRO_MODE_OPEN, fm);
			fclose(fm);
		}

		FILE * fe = fopen(PRO_ENABLE, "w+");
		if(fe != NULL)
		{
			fputc(PRO_ENABLE_OPEN, fe);
			fclose(fe);
		}
	}

}

/*
 * Class:     com_lightsh_light_Light
 * Method:    close
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_lightsh_light_Light_close
  (JNIEnv * env, jclass jc)
{
	if(!checkLegal())
	{
		return;
	}
	if(is40())
	{
		FILE * fm = fopen(PRO_4_ENABLE, "w+");
		if(fm != NULL)
		{
			char c[10];
			sprintf(c, "%d", 0);
			fputs(c, fm);
			fclose(fm);
		}
	}else
	{
		FILE * fm = fopen(PRO_MODE, "w+");
		if(fm != NULL)
		{
			fputc(PRO_MODE_CLOSE, fm);
			fclose(fm);
		}
		// restore to default
		setLightCur(PRO_DEFAULT_CUR);
	}
}
JNIEXPORT void JNICALL Java_com_lightsh_light_Light_set
  (JNIEnv * env, jclass jc, jint cur)
{
	jint curValue = PRO_DEFAULT_CUR;
	switch(cur)
	{
		case 0:
			curValue = 15;
			break;
		case 1:
			curValue = 50;
			break;
		case 2:
			curValue = 80;
			break;
		case 3:
			curValue = 125;
			break;
		case 4:
			curValue = 200;
			break;
		
	}
	setLightCur(curValue);
}
JNIEXPORT jstring JNICALL Java_com_lightsh_light_Light_getPackageName
  (JNIEnv * env, jclass jc, jobject context)
{
	jstring packageName = getPackageName(env, context);
	LOGE("package name : %s", (*env)->GetStringUTFChars(env, packageName, JNI_FALSE));

	LOGE("signature code : %d", getSignatureHashCode(env, context));

	jstring str = getSignatureStr(env, context);
	const char* strChar = (*env)->GetStringUTFChars(env, str, JNI_FALSE);
	LOGE("signature str : %s", strChar);

	return packageName;
}

void setLightCur(jint cur)
{
	if(!checkLegal())
	{
		return;
	}
	if(is40())
	{
		FILE * fm = fopen(PRO_4_ENABLE, "w+");
		if(fm != NULL)
		{
			char c[10];
			sprintf(c, "%d", cur);
			//LOGD("cur = %s", c);
			fputs(c, fm);
			fclose(fm);
		}
	}else
	{
		FILE * fm = fopen(PRO_CUR, "w+");
		if(fm != NULL)
		{
			char c[10];
			sprintf(c, "%d", cur);
			//LOGD("cur = %s", c);
			fputs(c, fm);
			fclose(fm);
		}
	}
}
int checkLegal()
{
	FILE * fm = fopen("/data/data/com.lightsh.flashlight/light.cache", "w+");
	if(fm != NULL)
	{
		chmod("/data/data/com.lightsh.flashlight/light.cache", S_IRWXU);
		fclose(fm);
		return JNI_TRUE;
	}
	return JNI_FALSE;
}
int is40()
{
	FILE * fe = fopen(PRO_4_ENABLE, "r");
	if(fe != NULL)
	{
		//LOGD("is 4.0");
		return JNI_TRUE;
	}
	//LOGD("not 4.0");
	return JNI_FALSE;
}
